﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core.DS
{
    public static partial class classes
    {
        public static dynamic importStudentList(int cid, int[] nos, string[] sids, string[] names, bool[] sexs, bool cleanFirst)
        {
            var sql = new Sql.SqlDataContext();
            if (!sql.DSClass.Any(row => row.cid == cid)) return new
            {
                success = false,
                msg = "班级不存在"
            };

            if (cleanFirst) sql.DSClassStudent.DeleteAllOnSubmit(sql.DSClassStudent.Where(row => row.cid == cid));
            sql.SubmitChanges();

            for (int i = 0; i < sids.Length; ++i)
            {
                Sql.DSClassStudent s = null;
                bool isnew = false;
                if (!cleanFirst)
                    s = sql.DSClassStudent.FirstOrDefault(row => row.cid == cid && row.sid == sids[i]);
                if (s == null)
                {
                    s = new Sql.DSClassStudent();
                    isnew = true;
                }

                s.cid = cid;
                s.NO = nos[i];
                s.sid = sids[i];
                s.name = names[i];
                s.sex = sexs[i];
                if (isnew) sql.DSClassStudent.InsertOnSubmit(s);
            }
            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
